package com.github.aloxc.plugin.restplus.common;


import com.github.aloxc.plugin.restplus.navigation.action.RestServiceItem;
import com.github.aloxc.plugin.restplus.navigator.RestServiceProject;
import com.github.aloxc.plugin.restplus.resolver.JaxrsResolver;
import com.github.aloxc.plugin.restplus.resolver.MicronautResolver;
import com.github.aloxc.plugin.restplus.resolver.ServiceResolver;
import com.github.aloxc.plugin.restplus.resolver.SpringResolver;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiMethod;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

/**
 * 服务相关工具类
 * @author liyh
 */
public class ServiceHelper {
    PsiMethod psiMethod;

    public ServiceHelper(PsiMethod psiMethod) {
        this.psiMethod = psiMethod;
    }

    public static List<RestServiceProject> buildRestServiceProjectListUsingResolver(Project project) {
        List<RestServiceProject> serviceProjectList = new ArrayList<>();
        Module[] modules = ModuleManager.getInstance(project).getModules();
        for (Module module : modules) {
            List<RestServiceItem> restServices = buildRestServiceItemListUsingResolver(project,module);
            if (restServices.size() > 0) {
                serviceProjectList.add(new RestServiceProject(module.getName(), restServices));
            }
        }
        return serviceProjectList;
    }

    public static List<RestServiceItem> buildRestServiceItemListUsingResolver(Project project,Module module) {
        List<RestServiceItem> itemList = new ArrayList<>();

        SpringResolver springResolver = new SpringResolver(module);
        JaxrsResolver jaxrsResolver = new JaxrsResolver(module);
        MicronautResolver micronautResolver = new MicronautResolver(module);
        ServiceResolver[] resolvers = {springResolver,jaxrsResolver,micronautResolver};
//        ServiceResolver[] resolvers = {micronautResolver};
        for (ServiceResolver resolver : resolvers) {
            List<RestServiceItem> allSupportedServiceItemsInModule = resolver.findAllSupportedServiceItemsInModule();
            itemList.addAll(allSupportedServiceItemsInModule);
        }
//        addSwaggerList(project,itemList);
        return itemList;
    }

//    private static void addSwaggerList(Project project,List<RestServiceItem> itemList){
//        RestConfig restConfig = RestConfig.loadConfig(project);
//        List<SwaggerConfig> swaggerConfigList = restConfig.getSwaggerConfigList();
//        List<RestServiceProject> projects = new ArrayList<>();
//        swaggerConfigList.stream().forEach(swaggerConfig -> {
//            swaggerConfig.getSwaggerRequestList().stream().forEach(swaggerRequest -> {
//                RestServiceItem item = new RestServiceItem(null, swaggerRequest.getMethod().name(), swaggerRequest.getUrl(), swaggerRequest.getMethod() == HttpMethod.POST_BODY);
//                item.setSwagger(true);
//                itemList.add(item);
//            });
//        });
//    }
    @NotNull
    public static List<RestServiceItem> buildRestServiceItemListUsingResolver(Project project) {
        List<RestServiceItem> itemList = new ArrayList<>();
        Module[] modules = ModuleManager.getInstance(project).getModules();
        for (Module module : modules) {
            List<RestServiceItem> restServices = buildRestServiceItemListUsingResolver(project,module);
            if (restServices.size() > 0) {
                itemList.addAll(restServices);
            }
        }
        return itemList;
    }
}
